package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constrant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.OrderSetting;
import com.itheima.pojo.Setmeal;
import com.itheima.service.OrderService;
import com.itheima.service.OrderSettingService;
import com.itheima.service.SetmealService;
import com.itheima.utils.POIUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.*;

/**
 * @author FCJ
 * @date 2020-01-05 11:27
 */
@RestController
@RequestMapping("/ordersetting")

public class OrderSettingController {
    @Reference
    private OrderSettingService orderSettingService;
    @Reference
    private OrderService orderService;
    @Reference
    private SetmealService setmealService;

    @RequestMapping("/upload")
    public Result upload(@RequestParam("excelFile") MultipartFile excelFile) {
        try {
            List<String[]> list = POIUtils.readExcel(excelFile);
            List<OrderSetting> data = new ArrayList<>();
            for (String[] s : list) {
                String orderDate = s[0];
                String number = s[1];
                OrderSetting orderSetting = new OrderSetting(new Date(orderDate), Integer.parseInt(number));
                data.add(orderSetting);
            }
            orderSettingService.addOrderSetting(data);
            return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.IMPORT_ORDERSETTING_FAIL);

        }

    }

    @RequestMapping("/findOrderListByMonth")
    public Result findOrderListByMonth(String date) {
        try {
            List<Map> list = orderSettingService.findOrderListByMonth(date);
            return new Result(true, MessageConstant.GET_ORDERSETTING_SUCCESS, list);
        } catch (Exception e) {
            return new Result(false, MessageConstant.GET_ORDERSETTING_FAIL);
        }


    }

    @RequestMapping("/handleOrderSet")
    public Result handleOrderSet(@RequestBody OrderSetting orderSetting) {
        try {
            orderSettingService.updateOneOrderSet(orderSetting);
            return new Result(true, MessageConstant.ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            return new Result(false, MessageConstant.ORDERSETTING_FAIL);
        }
    }

    //根据条件查询分页
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = orderService.pageQuery(queryPageBean);
        return pageResult;
    }

    //添加检查组
    @RequestMapping("/add")
    public Result add(@RequestBody Map map, Integer[] setmealIds) {
        try {
            orderService.add(map, setmealIds);
            return new Result(true, "添加预约成功");
        } catch (Exception e) {
            return new Result(false, "添加预约失败");
        }

    }

    @RequestMapping("/deleteOrderById")
    public Result deleteOrderById(Integer id) {
        try {
            orderService.deleteOrderById(id);
            return new Result(true, "删除预约成功");
        } catch (Exception e) {
            return new Result(false, "删除预约失败");
        }

    }
    //添加检查组
    @RequestMapping("/edit")
    public Result edit(@RequestBody Map map) {
        try {
             orderService.update(map);
            return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            return new Result(false, MessageConstant.EDIT_CHECKGROUP_FAIL);
        }

    }
    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
            HashMap<String, Object> map = new HashMap<>();
            Map order = orderService.findById(id);
            List<Integer> setmealIds = new ArrayList<>();
            setmealIds.add((Integer) order.get("setmeal_id"));
            List<Setmeal> setmeals = setmealService.findAll();
            map.put("order",order);
            map.put("setmealIds",setmealIds);
            map.put("setmeals",setmeals);
            return new Result(true, "查询预约成功",map);
        } catch (Exception e) {
            return new Result(false, "查询预约失败");
        }

    }
}